package com.cash.loan.modular.cash.service.impl;

import com.cash.loan.common.result.PageResult;
import com.cash.loan.common.upload.QiniuService;
import com.cash.loan.common.util.PageBeanUtil;
import com.cash.loan.modular.cash.dao.LoanCateMapper;
import com.cash.loan.modular.cash.model.LoanProduct;
import com.cash.loan.modular.cash.service.CateService;
import com.cash.loan.modular.cash.transfer.vo.ProductListVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description
 * @date 2018年10月03 9:43
 */
@Service
public class CateServiceImpl implements CateService {

    private static final Integer UI_TYPE = 2;

    @Autowired
    private LoanCateMapper loanCateMapper;

    @Autowired
    private QiniuService qiniuService;

    @Override
    public PageResult<ProductListVo> getProductListByCateId(Integer cateId,
                                                      Integer currentPage,Integer pageSize,Integer uiType) {
        PageHelper.startPage(currentPage,pageSize);
        List<LoanProduct> productList = loanCateMapper.selectProductListByCateId(cateId);
        if(productList == null || productList.size() == 0) {
            List<ProductListVo> list = new ArrayList<>();
            PageResult<ProductListVo> pageResult = new PageResult<>();
            pageResult.setDataList(list);
        }

        if(UI_TYPE.equals(uiType)) {
            productList = productList.stream()
                    .filter(loanProduct -> Integer.valueOf(0).equals(loanProduct.getDownloadFlag()))
                    .collect(Collectors.toList());
        }

        List<ProductListVo> productListVoList = new ArrayList<>();
        productList.stream().forEach(loanProduct -> {
            ProductListVo listVo = new ProductListVo();
            listVo.setProductId(loanProduct.getId());
            listVo.setProductName(loanProduct.getProductName());
            listVo.setProductIconUrl(qiniuService.getUrl(loanProduct.getProductIconUrl()));
            listVo.setProductSellPoint(loanProduct.getProductSellPoint());
            listVo.setDayLoanRate(loanProduct.getDayLoanRate());
            StringBuilder dateTerm = new StringBuilder("");
            if(loanProduct.getMinLoanTerm() > 30) {
                dateTerm.append(loanProduct.getMinLoanTerm() / 30).append("月-");
            } else {
                dateTerm.append(loanProduct.getMinLoanTerm()).append("天-");
            }
            if(loanProduct.getMaxLoanTerm() > 30) {
                dateTerm.append(loanProduct.getMaxLoanTerm() / 30).append("月");
            } else {
                dateTerm.append(loanProduct.getMaxLoanTerm()).append("天");
            }
            listVo.setLoanTerm(dateTerm.toString());
            listVo.setMaxLoanAmount(loanProduct.getMaxLoanAmount().toString());
            productListVoList.add(listVo);
        });
        PageResult pageResult = PageBeanUtil.toPageResult(productList);
        pageResult.setDataList(productListVoList);
        return pageResult;
    }

    @Override
    public List<Map<String, Object>> getCateList() {
        List<Map<String,Object>> cateList = loanCateMapper.getCateList();
        return cateList;
    }

}
